$(function () {
    $("#backPage").load("/html/backPage.html");
    getRoleList();
})
// 查询所有用户
function findAll() {
    let data = {
        key: $("#selectKey").val(),
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        role: $.trim($("#selectRole").val()),
        state: $.trim($("#selectState").val()),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    console.log(data)
    let result = jsonGetValue("/user/getUserPermissionList", data);
    console.log(result);
    setAllData(result.data.list);
    $("#total").html(result.data.count);
}
// 给用户列表赋值
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            "        <td>"+ data[i].id +"</td>\n" +
            "        <td>"+ data[i].userCode +"</td>\n" +
            "        <td>"+ (data[i].name == undefined?"暂无":data[i].name)  +"</td>\n" +
            "        <td>"+ data[i].roleName +'</td>\n';
        if (data[i].state == "normal") {
            html += "<td class='yes'><div onclick=\"changeShowState('" + data[i].userCode + "','frozen')\"><span>"+ data[i].userState +"</span><span class='iconfont'>&#xe60d</span></div></td>\n";
        } else if (data[i].state == "frozen") {
            html += "<td class='no'><div onclick=\"changeShowState('" + data[i].userCode + "','normal')\"><span>"+ data[i].userState +"</span><span class='iconfont'>&#xe600</span></div></td>\n";
        }
        html += "    <td>\n" +
            "            <div onclick=\"distribute('"+ data[i].id +"')\">" +
            '            <a href="#distribute-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-distribute">&#xe696;</i></a></div>\n' +
            "        </td>\n" +
            "    </tr>";
    }

    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:nth-child(12) > div").css({"cursor": "pointer","display": "flex","align-items": "center"})

}

// 修改用户状态
function changeShowState(userCode,state){
    let data = {
        userCode: userCode,
        state:state
    };
    let result = jsonPost("/user/updateState", data);
    console.log(result);
    if (result.data.data == 1) {
        page('go');
    } else {
        alert("修改状态失败");
    }
}

// 获取角色
function getRoleList() {
    let result = jsonGet("/role/getRoleList");
    console.log(result);
    setRoleListData(result.data.data);
}
// 给角色下拉列表赋值
function setRoleListData(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].id +'">'+ data[i].name +'</option></div>';
    }
    $("#selectState").html(html);
    getStateByCode();
}

// 从码表获取状态
function getStateByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"user_state"});
    console.log(result);
    setStateDataByCode(result.data.data);
}
// 给状态下拉列表赋值
function setStateDataByCode(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].code +'">'+ data[i].key +'</option></div>';
    }
    $("#selectState").html(html);
    findUserByUserCode();
}

// 查询点击事件
function select() {
    page("home");
}
// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#selectTeam").val("all");
    $("#selectEducation").val("all");
    $("#selectState").val("all");
    $("#pageSize").val(5);
    $("#pageNum").val(1);
}